package net.lab1024.sa.admin.module.flowable.demo;

import cn.hutool.extra.spring.SpringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.module.flowable.domain.vo.FlowTaskVo;
import net.lab1024.sa.admin.module.flowable.service.IFlowTaskService;
import org.springframework.context.ApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;

/**
 * @author chenqiang
 * @date 2022年03月15日16:22
 */
@Slf4j
public class BasicTaskListener {
    private final ThreadPoolTaskExecutor threadPoolTaskExecutor = SpringUtil.getBean("threadPoolTaskExecutor");
    private final IFlowTaskService flowTaskService = SpringUtil.getBean(IFlowTaskService.class);

    public void complete(String pid, String taskId, Map<String, Object> resultMap, String flowType, String comment) {
        threadPoolTaskExecutor.execute(() -> {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            FlowTaskVo flowTaskVo = new FlowTaskVo();
            flowTaskVo.setInstanceId(pid);
            flowTaskVo.setTaskId(taskId);
            flowTaskVo.setValues(resultMap);
            flowTaskVo.setComment(comment);
            flowTaskVo.setFlowType(flowType);
            flowTaskService.complete(flowTaskVo);
        });
    }
}
